package com.jfl.web.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysDictType;
import com.jfl.system.SysDictTypeService;
import com.jfl.utils.ConvertRequestUtil;

/**
 * 数据字典信息
 * 
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictTypeController extends AbstractController
{
    
    @Reference(version = "${jfl.version}")
    private SysDictTypeService sysDictTypeService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /** 
     * 界面
     * @return
     */
    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dictType()
    {
        return Constants.MODULE_SYS_DICT_TYPE_PREFIX + "type";
    }
    
    /** 
     * 列表
     * @param dictType
     * @return
     */
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public ResponseEntity<ModelMap> list(SysDictType dictType)
    {
        PageInfo<SysDictType> pageInfo = this.sysDictTypeService.selectList(ConvertRequestUtil.pageRequest(), dictType);
        return setSuccessModelMap(pageInfo);
    }
    
    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add()
    {
        return Constants.MODULE_SYS_DICT_TYPE_PREFIX + "add";
    }
    
    /**
     * 新增保存字典类型
     */
    @Log(module = Module.DICT, value = "添加数据字典", type = LogType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> addSave(SysDictType dict)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        dict.setCreateBy(ShiroUtils.getCurrentUserId());
        this.sysDictTypeService.update(dict);
        return setSuccessModelMap();
    }
    
    /**
     * 修改字典类型
     * 
     * @param dictId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        mmap.put("dict", this.sysDictTypeService.queryById(dictId));
        return Constants.MODULE_SYS_DICT_TYPE_PREFIX + "edit";
    }
    
    /**
     * 修改保存字典类型
     */
    @Log(module = Module.DICT, value = "修改数据字典", type = LogType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> editSave(SysDictType dict)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        dict.setUpdateBy(ShiroUtils.getCurrentUserId());
        this.sysDictTypeService.update(dict);
        return setSuccessModelMap();
    }
    
    /** 
     * 删除
     * @param ids
     * @return
     */
    @Log(module = Module.DICT, value = "删除数据字典", type = LogType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(String ids)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysDictTypeService.deleteByIds(ids);
        return setSuccessModelMap();
    }
    
    /**
     * 查询字典详细
     * 
     * @param dictId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        mmap.put("dict", sysDictTypeService.queryById(dictId));
        mmap.put("dictList", sysDictTypeService.queryList(Maps.newHashMap()));
        return "system/dict/data/data";
    }
    
    /**
     * 校验字典类型
     * 
     * @param dictType
     * @return
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(SysDictType dictType)
    {
        return sysDictTypeService.checkDictTypeUnique(dictType);
    }
}
